package com.ruoyi.web.controller.basic;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.basic.service.JyAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.JyArea;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域信息信息Controller
 *
 * @author aulbrother
 * @date 2024-04-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basic/area")
public class AreaController extends BaseController {
    private final JyAreaService jyAreaService;

    /**
     * 查询区域信息信息列表
     */
    @PreAuthorize("@ss.hasPermi('basic:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(JyArea jyArea) {
        startPage();
        List<JyArea> list = jyAreaService.selectJyAreaList(jyArea);
        return getDataTable(list);
    }

    /**
     * 导出区域信息信息列表
     */
    @PreAuthorize("@ss.hasPermi('basic:area:export')")
    @Log(title = "区域信息信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JyArea jyArea) throws IOException {
        List<JyArea> list = jyAreaService.selectJyAreaList(jyArea);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "区域信息信息数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JyArea.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取区域信息信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jyAreaService.selectJyAreaById(id));
    }

    /**
     * 新增区域信息信息
     */
    @PreAuthorize("@ss.hasPermi('basic:area:add')")
    @Log(title = "区域信息信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JyArea jyArea) {
        return toAjax(jyAreaService.insertJyArea(jyArea));
    }

    /**
     * 修改区域信息信息
     */
    @PreAuthorize("@ss.hasPermi('basic:area:edit')")
    @Log(title = "区域信息信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JyArea jyArea) {
        return toAjax(jyAreaService.updateJyArea(jyArea));
    }

    /**
     * 删除区域信息信息
     */
    @PreAuthorize("@ss.hasPermi('basic:area:remove')")
    @Log(title = "区域信息信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jyAreaService.deleteJyAreaByIds(ids));
    }
}
